home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZDebug.cpp < prev    next >
Text File  |  1997-08-15  |  8KB  |  404 lines

  1. /*
  2.  *  File:       ZDebug.cpp
  3.  *  Summary:       Debugging functions and macros.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1996-1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):    
  10.  *
  11.  *         <6>     8/14/97    JDJ        TRACE functions use a larger buffer.
  12.  *         <5>     6/12/97    JDJ        BreakStrToDebugger strips trailing carriage returns.
  13.  *         <4>     1/03/97    JDJ        Added a comment to operator new override.
  14.  *         <3>    12/10/96    JDJ        Turned off intense debugging.
  15.  *         <2>    12/05/96    JDJ        Exposed the break to debugger functions.
  16.  *         <1>     1/13/96    JDJ        Created.
  17.  */
  18.  
  19. #include <ZDebug.h>
  20.  
  21. #include <String>
  22. #include <String.h>
  23. #include <StdArg.h>
  24. #include <StdIo.h>
  25.  
  26. #include <AERegistry.h>
  27. #include <AppleEvents.h>
  28. #include <Controls.h>
  29. #include <Memory.h>
  30. #include <Processes.h>
  31. #include <TextEdit.h>
  32. #include <Types.h>
  33. #include <Windows.h>
  34.  
  35. #include <ZTrace.h>
  36. #include <ZTypes.h>
  37.  
  38. #if ASSERTS_THROW
  39. #include <ZExceptions.h>
  40. #endif
  41.  
  42.  
  43. //---------------------------------------------------------------
  44. //
  45. // Globals
  46. //
  47. //---------------------------------------------------------------
  48. bool gMonkeyLives = false;                
  49.  
  50. #if DEBUG
  51. bool gIntenseDebugging = false;
  52. bool gBreakOnAssert = true;
  53.  
  54. extern "C" Boolean SIOUXIsAppWindow(const WindowRef);    // ・・・ハundocumented SIOUX function...
  55. #endif
  56.  
  57.  
  58. //---------------------------------------------------------------
  59. //
  60. // BreakToDebugger
  61. //
  62. //---------------------------------------------------------------
  63. void BreakToDebugger()
  64. {
  65. #ifdef powerc
  66.     Debugger();
  67. #else 
  68.     Debugger68k();
  69. #endif    
  70. }
  71.  
  72.  
  73. //---------------------------------------------------------------
  74. //
  75. // BreakStrToDebugger
  76. //
  77. //---------------------------------------------------------------
  78. void BreakStrToDebugger(const char* mesg)
  79. {
  80.     unsigned char s[256];
  81.     
  82.     ulong len = strlen(mesg);
  83.     if (len > 255)
  84.         len = 255;
  85.         
  86.     s[0] = (Byte) len;
  87.     BlockMoveData(mesg, s+1, len);
  88.  
  89.     // Strip off trailing carriage returns.
  90.     while (s[0] > 0 && (s[s[0]] == '¥n' || s[s[0]] == '¥r'))
  91.         --s[0];
  92.  
  93. #if powerc
  94.     DebugStr(s);
  95. #else 
  96.     DebugStr68k(s);
  97. #endif    
  98. }
  99.  
  100.  
  101. //---------------------------------------------------------------
  102. //
  103. // AssertFailed
  104. //
  105. //---------------------------------------------------------------
  106. #if DEBUG || ASSERTS_THROW
  107. void AssertFailed(const char* expr, const char* file, int line)
  108. {
  109.     char mesg[256];                            
  110.  
  111.     sprintf(mesg, "ASSERT(%s) in %s at line %d failed.", expr, file, line);
  112.     
  113. #if DEBUG
  114.     if (gBreakOnAssert)
  115.         BreakStrToDebugger(mesg);
  116. #endif
  117.  
  118. #if ASSERTS_THROW
  119.     throw TAssertException(mesg); 
  120. #endif
  121. }
  122. #endif    // DEBUG || ASSERTS_THROW
  123.  
  124.  
  125. #if DEBUG
  126.  
  127. //---------------------------------------------------------------
  128. //
  129. // DEBUGSTR
  130. //
  131. //---------------------------------------------------------------
  132. void DEBUGSTR(const char* format, ...)
  133. {
  134.     char mesg[2048];                            
  135.  
  136.     va_list args;
  137.     va_start(args, format);
  138.     vsprintf(mesg, format, args);
  139.     va_end(args);
  140.     
  141.     BreakStrToDebugger(mesg);
  142. }
  143.  
  144.  
  145. //---------------------------------------------------------------
  146. //
  147. // DEBUGSTR_IF
  148. //
  149. //---------------------------------------------------------------
  150. void DEBUGSTR_IF(bool cond, const char* format, ...)
  151. {
  152.     if (cond) {
  153.         char mesg[2048];                            
  154.     
  155.         va_list args;
  156.         va_start(args, format);
  157.         vsprintf(mesg, format, args);
  158.         va_end(args);
  159.         
  160.         BreakStrToDebugger(mesg);
  161.     }
  162. }
  163.  
  164.  
  165. //---------------------------------------------------------------
  166. //
  167. // TRACE
  168. //
  169. //---------------------------------------------------------------
  170. void TRACE(const char* format, ...)
  171. {
  172.     char mesg[2048];                            
  173.  
  174.     va_list args;
  175.     va_start(args, format);
  176.     vsprintf(mesg, format, args);
  177.     va_end(args);
  178.  
  179.     UTraceFlow::Trace(mesg);
  180. }    
  181.  
  182.  
  183. //---------------------------------------------------------------
  184. //
  185. // TRACE_IF
  186. //
  187. //---------------------------------------------------------------
  188. void TRACE_IF(bool cond, const char* format, ...)
  189. {
  190.     if (cond) {
  191.         char mesg[2048];                            
  192.     
  193.         va_list args;
  194.         va_start(args, format);
  195.         vsprintf(mesg, format, args);
  196.         va_end(args);
  197.     
  198.         UTraceFlow::Trace(mesg);
  199.     }
  200. }
  201.  
  202.  
  203. //---------------------------------------------------------------
  204. //
  205. // TRACEFLOW
  206. //
  207. //---------------------------------------------------------------
  208. void TRACEFLOW(const char* category, const char* format, ...)
  209. {
  210.     char mesg[2048];                            
  211.  
  212.     va_list args;
  213.     va_start(args, format);
  214.     vsprintf(mesg, format, args);
  215.     va_end(args);
  216.  
  217.     UTraceFlow::TraceFlow(category, mesg);
  218. }
  219.  
  220.  
  221. //---------------------------------------------------------------
  222. //
  223. // TRACEFLOW_IF
  224. //
  225. //---------------------------------------------------------------
  226. void TRACEFLOW_IF(bool cond, const char* category, const char* format, ...)
  227. {
  228.     if (cond) {
  229.         char mesg[2048];                            
  230.     
  231.         va_list args;
  232.         va_start(args, format);
  233.         vsprintf(mesg, format, args);
  234.         va_end(args);
  235.     
  236.         UTraceFlow::TraceFlow(category, mesg);
  237.     }
  238. }
  239.  
  240.  
  241. //---------------------------------------------------------------
  242. //
  243. // Length
  244. //
  245. //---------------------------------------------------------------
  246. #pragma profile off
  247. static long Length(const char* str)        // interrupt code
  248. {
  249.     long len = 0;
  250.     
  251.     while (*str++)
  252.         len++;
  253.     
  254.     return len;
  255. }
  256. #pragma profile reset
  257.  
  258.  
  259. //---------------------------------------------------------------
  260. //
  261. // Append (char*, const char*, long)
  262. //
  263. //---------------------------------------------------------------
  264. #pragma profile off
  265. static void Append(char* buffer, const char* str, long bufferLen)        // interrupt code
  266. {
  267.     if (buffer != nil && str != nil) {
  268.         long index = Length(buffer);
  269.         
  270.         while (*str && index+1 < bufferLen)
  271.             buffer[index++] = *str++;
  272.             
  273.         buffer[index] = '¥0';
  274.     }
  275. }
  276. #pragma profile reset
  277.  
  278.  
  279. //---------------------------------------------------------------
  280. //
  281. // Append (char*, long, long)
  282. //
  283. //---------------------------------------------------------------
  284. #pragma profile off
  285. static void Append(char* buffer, long num, long bufferLen)        // interrupt code
  286. {
  287.     if (buffer != nil) {
  288.         // Convert num to a str starting at index.
  289.         char str[12];
  290.         str[11] = '¥0';
  291.         
  292.         bool negative = num < 0;
  293.         
  294.         short index = 11;
  295.         do {
  296.             long digit = num % 10;
  297.             num /= 10;
  298.             
  299.             str[--index] = (char) ('0' + (negative ? -digit : digit));
  300.         } while (num != 0 && index > 0);
  301.  
  302.         if (negative && index > 0)
  303.             str[--index] = '-';
  304.             
  305.         // Do the concatenation
  306.         Append(buffer, str + index, bufferLen);
  307.     }
  308. }
  309. #pragma profile reset
  310.  
  311.  
  312. //---------------------------------------------------------------
  313. //
  314. // SAFE_DEBUGSTR (const char*)
  315. //
  316. //---------------------------------------------------------------
  317. #pragma profile off
  318. void SAFE_DEBUGSTR(const char* str)            // interrupt code
  319. {
  320.     unsigned char mesg[81];                    // MACSBUG seems to choke on large strings...
  321.     
  322.     long len = Length(str);        
  323.     if (len > 80)
  324.         len = 80;
  325.         
  326.     mesg[0] = (Byte) len;
  327.     BlockMoveData(str, mesg+1, len);
  328.     
  329. #ifdef powerc
  330.     DebugStr(mesg);
  331. #else 
  332.     SysBreakStr(mesg);
  333. #endif    
  334. }
  335. #pragma profile reset
  336.  
  337.  
  338. //---------------------------------------------------------------
  339. //
  340. // SAFE_DEBUGSTR (const char*)
  341. //
  342. //---------------------------------------------------------------
  343. #pragma profile off
  344. void SAFE_DEBUGSTR(const char* mesg, long num)        // interrupt code
  345. {
  346.     char buffer[256];
  347.     
  348.     buffer[0] = '¥0';
  349.     
  350.     Append(buffer, mesg, 256);
  351.     Append(buffer, num, 256);
  352.     
  353.     SAFE_DEBUGSTR(buffer);
  354. }
  355. #pragma profile reset
  356.  
  357.  
  358. //---------------------------------------------------------------
  359. //
  360. // SafeAssertFailed
  361. //
  362. //---------------------------------------------------------------
  363. #pragma profile off
  364. void SafeAssertFailed(const char* expr, const char* file, int line)        // interrupt code
  365. {
  366.     char buffer[256];
  367.     
  368.     buffer[0] = '¥0';
  369.     
  370.     Append(buffer, "ASSERT(", 256);
  371.     Append(buffer, expr, 256);
  372.     Append(buffer, ") in ", 256);
  373.     Append(buffer, file, 256);
  374.     Append(buffer, " at line ", 256);
  375.     Append(buffer, line, 256);
  376.     Append(buffer, " failed.", 256);
  377.     
  378.     SAFE_DEBUGSTR(buffer);
  379. }
  380. #pragma profile reset
  381.  
  382.  
  383. //---------------------------------------------------------------
  384. //
  385. // GetSIOUXWindow
  386. //
  387. // If you're using console.stubs.c you may need to add SIOUXIsAppWindow
  388. // to console.stubs.c (just return false).
  389. //
  390. //---------------------------------------------------------------
  391. WindowRef GetSIOUXWindow()
  392. {
  393.     WindowRef window = FrontWindow();
  394.     
  395.     while (window != nil && !SIOUXIsAppWindow(window))
  396.         window = GetNextWindow(window);
  397.     
  398.     return window;
  399. }
  400.  
  401.  
  402. #endif    // DEBUG
  403.  
  404.